package com.xinchao.ck_mips.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.xinchao.ck_mips.GlideApp;
import com.xinchao.ck_mips.R;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by zhangbw on 2018/12/18.
 */

public class GlideTool {

    /**
     * 加载图片
     *
     * @param imageView
     * @param uri
     */
    public static void loadImage(ImageView imageView, String uri) {
        try {
            GlideApp.with(imageView)
                    .load(uri)
                    //  .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .placeholder(imageView.getDrawable())
                    .into(imageView);
        } catch (Exception e) {

        }

    }

    /**
     * 加载图片
     *
     * @param imageView
     * @param resourceId
     */
    public static void loadImage(ImageView imageView, int resourceId) {
        try {
            GlideApp.with(imageView)
                    .load(resourceId)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .placeholder(imageView.getDrawable())
                    .into(imageView);
        } catch (Exception e) {

        }

    }

    public static void loadImageTarget(ImageView imageView, String uri) {
        try {
            GlideApp.with(imageView).asBitmap()
                    .load(uri)
                    .placeholder(imageView.getDrawable())
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            int width = resource.getWidth();
                            int height = resource.getHeight();
                            imageView.post(() -> {
                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
                                imageView.setImageBitmap(resource);
                                imageView.setLayoutParams(layoutParams);

                            });
                        }
                    });
        } catch (Exception e) {

        }

    }

    /**
     * 加载图片
     *
     * @param imageView
     * @param uri
     */
    public static void loadImageWithCorners(ImageView imageView, String uri, float radius, RoundedCornersTransformation.CornerType cornerType) {
        try {

            RequestOptions myOptions = new RequestOptions()
                    .transform(new CenterCropRoundedCornersTransformation((int) (radius - imageView.getContext().getResources().getDimension(R.dimen.corner_1_5)), 0, cornerType));

            GlideApp.with(imageView)
                    .load(uri)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .placeholder(imageView.getDrawable())
                    .apply(myOptions)
                    .into(imageView);

        } catch (Exception e) {

        }

    }

    /**
     * 加载图片
     *
     * @param imageView
     * @param resourceId
     */
    public static void loadImageWithCorners(ImageView imageView, int resourceId, float radius, RoundedCornersTransformation.CornerType cornerType) {
        try {

            RequestOptions myOptions = new RequestOptions()
                    .transform(new CenterCropRoundedCornersTransformation((int) (radius - imageView.getContext().getResources().getDimension(R.dimen.corner_1_5)), 0, cornerType));

            GlideApp.with(imageView)
                    .load(resourceId)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .placeholder(imageView.getDrawable())
                    .apply(myOptions)
                    .into(imageView);

        } catch (Exception e) {

        }

    }

    public static class CenterCropRoundedCornersTransformation extends RoundedCornersTransformation {

        public CenterCropRoundedCornersTransformation(int radius, int margin) {
            super(radius, margin);
        }

        public CenterCropRoundedCornersTransformation(int radius, int margin, CornerType cornerType) {
            super(radius, margin, cornerType);
        }

        @Override
        protected Bitmap transform(Context context, BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return super.transform(context, pool, TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight), outWidth, outHeight);
        }
    }
}
